# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4

PortSystem                  1.0
PortGroup                   conflicts_build 1.0

# Keep relevant lines in sync between ImageMagick and p5-perlmagick.

# Before updating to a newer version, install phpNN-imagick. After updating, run `phpNN -v`. If the following warning appears, revbump php-imagick.
# PHP Warning:  Version warning: Imagick was compiled against Image Magick version XXXX but version YYYY is loaded. Imagick will run but may behave surprisingly in Unknown on line 0

name                        ImageMagick
version                     6.9.11-16
revision                    0
checksums                   rmd160  2402446464a320ffefbaedc193dd55cd90f60af4 \
                            sha256  6e9328e846dadf2b96832d02eddd3da9121f546ca86dbb7da15b8ae21f87ed22 \
                            size    9075792

categories                  graphics devel
maintainers                 {ryandesign @ryandesign}
license                     Apache-2
use_xz                      yes
platforms                   darwin
use_parallel_build          yes

description                 Tools and libraries to manipulate images in many formats

long_description            ImageMagick is a robust collection of tools and \
                            libraries to create, edit and compose bitmap images \
                            in a wide variety of formats. You can crop, resize, \
                            rotate, sharpen, color reduce or add effects or text \
                            or straight or curved lines to an image or image \
                            sequence and save your completed work in the same or \
                            differing image format. You can even create images \
                            from scratch. Image processing operations are \
                            available from the command line as well as through \
                            C, Ch, C++, Java, Perl, PHP, Python, Ruby and Tcl/Tk \
                            programming interfaces. Over 90 image formats are \
                            supported, including GIF, JPEG, JPEG 2000, PNG, PDF, \
                            PhotoCD and TIFF.

homepage                    https://legacy.imagemagick.org
master_sites                https://imagemagick.org/download/ \
                            http://mirror.checkdomain.de/imagemagick/ \
                            ftp://ftp.u-aizu.ac.jp/pub/graphics/image/ImageMagick/imagemagick.org/ \
                            ftp://sunsite.icm.edu.pl/packages/ImageMagick/

depends_lib                 port:bzip2 \
                            port:djvulibre \
                            port:xz \
                            port:jbigkit \
                            path:lib/libjpeg.dylib:jpeg \
                            port:lcms2 \
                            port:libpng \
                            port:libraw \
                            port:tiff \
                            port:webp \
                            port:zlib \
                            port:fftw-3 \
                            port:freetype \
                            port:fontconfig \
                            port:ghostscript \
                            port:libiconv \
                            port:libtool \
                            port:openjpeg \
                            port:openexr \
                            port:expat \
                            port:libxml2 \
                            port:libheif

patchfiles \
    noclparser.patch \
    ImageMagick-6.9.10-gimp210.diff

# Magick-config etc. use pkg-config
depends_lib-append          port:pkgconfig

depends_run                 port:urw-fonts

configure.ccache            no

use_autoreconf              yes
autoreconf.args             -fvi

configure.args              --enable-shared \
                            --enable-static \
                            --disable-silent-rules \
                            --with-frozenpaths \
                            --with-openexr \
                            --disable-hdri \
                            --with-dps \
                            --with-bzlib \
                            --with-djvu \
                            --with-fftw \
                            --with-fontconfig \
                            --with-gslib \
                            --with-jbig \
                            --with-jpeg \
                            --with-lcms \
                            --without-lzma \
                            --with-openjp2 \
                            --with-png \
                            --with-tiff \
                            --with-webp \
                            --with-zlib \
                            --with-modules \
                            --with-xml \
                            --with-heic \
                            --without-gcc-arch \
                            --without-perl \
                            --without-fpx \
                            --without-wmf \
                            --without-gvc \
                            --without-rsvg \
                            --without-lqr \
                            --without-pango \
                            --without-x \
                            --without-zstd \
                            --with-gs-font-dir=${prefix}/share/fonts/urw-fonts \
                            --disable-openmp

if {${os.platform} eq "darwin" && ${os.major} < 11} {
    configure.args-append   --disable-opencl
} else {
    configure.args-append   --enable-opencl
    # On case-insensitive filesystems, ImageMagick finds cryptlib's libcl and
    # tries to use it as if it were Apple's OpenCL, which fails; see #23354.
    if {[file exists ${prefix}/lib/libCL.dylib]} {
        conflicts_build         cryptlib
    }
}

variant natron conflicts x11 requires pango rsvg q32 hdri description {Natron version} {
    if {${configure.cxx_stdlib} eq "libstdc++"} {
        # configure with GCC 4.9 for proper OpenMP support
        configure.compiler macports-gcc-4.9
    } else {
        # libc++-based build
        # configure with clang-4.0 for proper OpenMP support
        if {${os.major} < 11} {
           configure.compiler macports-clang-4.0
        } else {
           configure.compiler macports-clang-5.0
        }
    }
    configure.args-append --enable-openmp
    depends_lib-delete      port:ghostscript
    configure.args-replace  --with-dps --without-dps
    configure.args-replace  --with-gslib --without-gslib
    depends_lib-delete       port:djvulibre
    configure.args-replace  --with-djvu --without-djvu
    depends_lib-delete       port:fftw-3
    configure.args-replace  --with-fftw --without-fftw
    depends_lib-delete       port:jbigkit
    configure.args-replace  --with-jbig --without-jbig
    depends_lib-delete       port:jbigkit
    configure.args-replace  --with-jbig --without-jbig
    depends_lib-delete       port:jpeg
    configure.args-replace  --with-jpeg --without-jpeg
    # PNG is required for povray,openraster,krita plugin, see https://github.com/olear/openfx-arena/commit/48064135604d1c2d07362bc16cecc7a16a640d3e#commitcomment-14356716
    # depends_lib-delete       port:png
    # configure.args-replace  --with-png --without-png
    depends_lib-delete       port:openjpeg
    configure.args-replace  --with-openjp2 --without-openjp2
    depends_lib-delete       port:openexr
    configure.args-replace  --with-openexr --without-openexr
    depends_lib-delete       port:tiff
    configure.args-replace  --with-tiff --without-tiff
    depends_lib-delete       port:webp
    configure.args-replace  --with-webp --without-webp
    # zlib is required by PNG
    #depends_lib-delete       port:zlib
    #configure.args-replace  --with-zlib --without-zlib
    depends_lib-delete       port:bzip2
    configure.args-replace  --with-bzlib --without-bzlib
    configure.args-replace  --enable-shared --disable-shared
    configure.args-replace  --with-modules --without-modules
    #pre-extract {
    #    if {${version} != "6.9.7-5"} {
    #        ui_msg "Natron variant requires ImageMagick version 6.9.7-5 (this is ${version}), please modify the Portfile."
    #        return -code error "incompatible ImageMagick version"
    #    }
    #}
}

# OpenMP support seems to be broken in gcc 4.2.1 and llvm-gcc-4.2 (OpenMP can only be used in the main thread):
# http://sourceforge.net/tracker/index.php?func=detail&aid=1964341&group_id=170962&atid=856209
# http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36242
# clang has proper OpenMP support
#   -- Frederic.Devernay@inria.fr
if {${configure.compiler} == "llvm-gcc-4.2" || ${configure.compiler} == "gcc-4.2" || ${configure.compiler} == "gcc-4.0"} {
    configure.args-append --disable-openmp
}

post-configure {
    reinplace -E {s|-arch [a-z0-9_]+||g} \
        ${worksrcpath}/magick/Magick-config \
        ${worksrcpath}/magick/MagickCore-config \
        ${worksrcpath}/Magick++/bin/Magick++-config \
        ${worksrcpath}/wand/MagickWand-config \
        ${worksrcpath}/wand/Wand-config
}

test.run                    yes
test.target                 check
test.env                    DYLD_LIBRARY_PATH=${worksrcpath}/magick/.libs

# ImageMagick uses .la files at runtime to find its coder modules.
destroot.delete_la_files    no

post-destroot {
    if {${configure.cxx_stdlib} eq "libc++"} {
        # The pkg-config files contain -lgomp instead of -L/opt/local/lib/libomp -omp
        foreach f [glob -directory ${destroot}${prefix}/lib/pkgconfig Magick*pc Wand*pc ImageMagick*pc] {
            reinplace -locale C {/Libs.private: /s:-lgomp:-L${prefix}/lib/libomp -lomp:g} ${f}
        }
    }
}

variant q32 description {Use Q32 quantum depth} {
    configure.args-append   --with-quantum-depth=32
}

variant hdri description {High Dynamic Range Imagery} {
    configure.args-replace  --disable-hdri --enable-hdri
}

variant graphviz description {Support Graphviz} {
    depends_lib-append      path:bin/dot:graphviz
    configure.args-replace  --without-gvc --with-gvc
}

variant lqr description {Support Liquid Rescale (experimental)} {
    depends_lib-append      port:liblqr
    configure.args-replace  --without-lqr --with-lqr
}

variant pango description {Support Pango} {
    depends_lib-append      path:lib/pkgconfig/pango.pc:pango
    configure.args-replace  --without-pango --with-pango
}

variant rsvg description {Support SVG using librsvg} {
    depends_lib-append      port:librsvg
    configure.args-replace  --without-rsvg --with-rsvg
}

variant wmf description {Support the Windows Metafile Format} {
    depends_lib-append      port:libwmf
    configure.args-replace  --without-wmf --with-wmf
}

variant x11 {
    depends_lib-append      port:xorg-libX11 \
                            port:xorg-libXext \
                            port:xorg-libXt
    configure.args-replace  --without-x --with-x
}

default_variants            +x11

variant xquartz {
    configure.args-append   --with-x --x-includes=/opt/X11/include --x-librari=/opt/X11/lib
    configure.args-delete   --without-x
}

livecheck.type              regex
livecheck.url               [lindex ${master_sites} 0]
livecheck.regex             ${name}-(6(?:\\.\\d+)+(?:-\\d+)?)\.tar
